package ge.telasi.tasks.ui.log;

import ge.telasi.tasks.LocaleManager;
import ge.telasi.tasks.TaskException;
import ge.telasi.tasks.ui.Platform;
import ge.telasi.tasks.ui.UIUtils;
import java.awt.Component;
import javax.ejb.EJBException;
import javax.ejb.NoSuchEJBException;
import javax.naming.NamingException;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

/**
 * @author dimitri
 */
public class LoggerUtils {

    /**
     * Clear logger.
     */
    public static void clearLogger(Logger logger, JComponent[] suspects) {
        // clear log message itself
        if (logger != null) {
            logger.clear();
        }
        // reset decoration of all fields to their default view
        if (suspects != null) {
            for (JComponent comp : suspects) {
                decorateDefault(comp);
            }
        }
    }

    /**
     * Display error in logger.
     */
    static void showError(Component parent, Logger logger, String error) {
        if (logger != null) {
            logger.showError(error);
        } else {
            LocaleManager lm = Platform.getDefault().getLocaleManager();
            String title = lm.getMessage("LoggerUtils.errorTitle");
            JLabel msg = new JLabel(error);
            JOptionPane.showMessageDialog(UIUtils.findShell(parent), msg, title, JOptionPane.ERROR_MESSAGE);
        }
    }

    /**
     * Focus on the error and decorate it.
     */
    static void focusError(Logger logger, Exception ex, String nameTo, JComponent[] from) {
        if (nameTo != null) {
            for (JComponent comp : from) {
                if (nameTo.equals(comp.getName())) {
                    comp.requestFocusInWindow();
                    decorateError(logger, ex, comp);
                }
            }
        }
    }

    public static void manageException(Component parent, Exception ex) {
        manageException(parent, null, ex);
    }

    /**
     * Manage exception.
     */
    public static void manageException(Component parent, Logger logger, Exception ex) {
        manageException(parent, logger, ex, null);
        ex.printStackTrace();
    }

    /**
     * Manage exception with focusing on the field which caused this exception.
     */
    public static void manageException(Component parent, Logger logger, Exception ex, JComponent[] suspects) {
        TaskException ee = isEE(ex);
        if (ee != null) {
            showError(parent, logger, ee.getMessage());
            if (suspects != null) {
                focusError(logger, ee, ee.getField(), suspects);
            }
        } else if (ex instanceof NamingException) {
            LocaleManager lm = Platform.getDefault().getLocaleManager();
            showError(parent, logger, lm.getMessage("LoggerUtils.NamingException"));
        } else if (ex instanceof NoSuchEJBException) {
            LocaleManager lm = Platform.getDefault().getLocaleManager();
            showError(parent, logger, lm.getMessage("LoggerUtils.NoSuchEJBException"));
        } else {
            showError(parent, logger, ex.toString());
        }
    }

    /**
     * Check exception whether it is an EasytaxException.
     */
    static TaskException isEE(Throwable ex) {
        if (ex instanceof EJBException) {
            EJBException ejbex = (EJBException) ex;
            if (ejbex.getCausedByException() != null) {
                return isEE(ejbex.getCausedByException());
            }
        } else if (ex instanceof TaskException) {
            return (TaskException) ex;
        } else if (ex.getCause() != null) {
            return isEE(ex.getCause());
        }
        return null;
    }

    static void decorateError(Logger logger, Exception ex, JComponent comp) {
        if (comp instanceof LoggableField) {
            LoggableField field = (LoggableField) comp;
            field.setLogger(logger);
            field.setException(ex);
        }
//        else if (comp instanceof JTextComponent || comp instanceof JComboBox || comp instanceof JPanel) {
//            comp.setBorder(BorderFactory.createLineBorder(Color.RED));
//        }
    }

    static void decorateDefault(JComponent comp) {
//        if (comp instanceof JTextComponent) {
//            comp.setBorder(new JTextField().getBorder());
//        } else if (comp instanceof JComboBox) {
//            comp.setBorder(new JComboBox().getBorder());
//        } else if (comp instanceof JPanel) {
//            comp.setBorder(null);
//        }
        if (comp instanceof LoggableField) {
            LoggableField field = (LoggableField) comp;
            field.setException(null);
        }
    }

}
